<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Article;
use App\Http\Model\Cate;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class CateController extends Controller
{
    //修改排序
    public function changeOrder()
    {
       $input = Input::except('_token');
       $input['cate_id'];
       $input['cate_order'];
//       通过cate_id找到要修改的分类记录
       $cate = Cate::find($input['cate_id']);
       $cate->cate_order = $input['cate_order'];
       $re = $cate->save();
       $data = [];
       if($re){
            $data['status'] = 0;
            $data['msg']  = '修改成功';
       }else{
           $data['status'] = 1;
           $data['msg']  = '修改成功';
       }
       return $data;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
//        获取要显示在分类列表页的数据
        $data =  (new Cate)->getTree();
        //返回分类列表页
        return view('admin.cate.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
//        调用分类模型的getTree方法，获取有缩进关系的分类数据
       $cates =  (new Cate)->getTree();
        //从数据库中查询出所有的分类
//        $cates = Cate::orderBy('cate_order','asc')->get();
//        dd($cates);

        return view('admin.cate.add',compact('cates'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //获取到用户提交过来的数据
        $input = $request->except('_token');
        //表单验证
        //将提交的数据保存到分类表中
//        insert into category('id','name','pid','path') values('1','新闻','0','0,1');
        $re = Cate::create($input);
//        $re = Cate::create(['cate_name'=>input['cate_name'],'cate_title'=>input['cate_title']....]);

        if($re){
            return redirect('admin/cate');
        }else{
            return back()->with('errors','分类添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        // 1 查询出当前分类下的所有文章
        $arts = Article::where('cate_id',$id)->get();
//        定义一个空字符串来接收每行的文章记录
        $str ='<table class="list_tab"><tr><th class="tc">ID</th><th>文章标题</th> <th>点击量</th><th>作者</th></tr>';

        foreach($arts as $k=>$v){
          $str.= "<tr>
               <td class='tc' style='width:30px'>{$v->art_id}</td>
               <td class='tc'>{$v->art_title}</td>
               <td class='tc'>{$v->art_view}</td>
               <td class='tc'>{$v->art_editor}</td>
              </tr>";
        }
        $str.= '</table>';
        // 2 将查询后的结果变成一个字符串格式的表格

        return $str;

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
//        判断当前分类下是否有子类
       $cate = Cate::find($id);
       $num =   Cate::where('cate_pid',$id)->count();
       //如果当前分类的pid为零，并且当前分类的id没有在pid字段出现过（没有子类·）
       if($cate->cate_pid == 0 && $num){
           $data=[
               'status'=>3,
               'msg'=>'当前分类下有子类，不能删除'
           ];
           return $data;
       }
        //找到要删除的记录
       $re =  $cate->delete();
       //判断删除是否成功
        if($re){
            $data=[
                'status'=>0,
                'msg'=>'删除成功'
            ];
        }else{
            $data=[
                'status'=>1,
                'msg'=>'删除失败'
            ];
        }
        return $data;
    }
}
